﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using OGS.Contracts;
using OGS.DataAccess;

namespace OGS.DataAccess
{
    /// <summary>
    /// Represents a class used for database related work of UOM
    /// </summary>
    public class DAUOM
    {

        private static readonly DAUOM instance;

        /// <summary>
        /// Initializes a static instance of the DAUOM class with no arguments.
        /// </summary>
        static DAUOM()
        {
            instance = new DAUOM();
        }

        /// <summary>
        /// Initializes a new instance of the DAUOM class with no arguments.
        /// </summary>
        private DAUOM() { }

        /// <summary>
        /// The Instance of DAUOM class
        /// </summary>
        public static DAUOM Instance
        {
            get
            {
                return instance;
            }
        }

        /// <summary>
        /// Saves the UOM
        /// </summary>
        /// <param name="objUOMInfo"> The UOMInfo Object </param>
        /// <returns>Success/Faliure</returns>
        public int SaveUOM(UOM objUOMInfo)
        {
            using (DBManager dbm = new DBManager("stp_Save_UOM"))
            {
                dbm.Add("@UOMID", objUOMInfo.ID, ParameterDirection.InputOutput);
                dbm.Add("@Name", objUOMInfo.Name);
                dbm.Add("@IsActive", objUOMInfo.IsActive);
                if (objUOMInfo.LastModifiedBy > 0)
                {
                    dbm.Add("@LastModifiedBy", objUOMInfo.LastModifiedBy);
                }
                dbm.Add("@LastModifiedDate", objUOMInfo.LastModifiedDate);

                dbm.ExecuteNonQuery();

                objUOMInfo.ID = (int)dbm.GetValueInInt("@UOMID");
                return Convert.ToInt32(dbm.ReturnValue);
            }
        }
               
        /// <summary>
        /// Get UOM Details
        /// </summary>
        /// <param name="ID"> The UOM ID </param>
        /// <returns>The UOM DataTable </returns>
        public UOM GetUOMDetails(int ID)
        {
            DataTable dt = new DataTable();
            using (DBManager dbm = new DBManager("stp_Get_UOMDetails"))
            {
                dbm.Add("@UOMID", ID);

                dt = dbm.GetDataTable();
            }

            UOM objUOMInfo = new UOM();
            if (dt.Rows.Count > 0)
            {
                objUOMInfo = PopulateUOM(dt.Rows[0]);
            }
            return objUOMInfo;
        }

        /// <summary>
        /// Search the UOM
        /// </summary>
        /// <param name="objUOMInfo"> The UOMInfo Object </param>
        /// <param name="objSearch"> The SearchAttributes Object </param>
        /// <returns>The UOM DataTable </returns>
        public List<UOM> SearchUOM(SearchAttributes objSearch)
        {
            DataTable dt = new DataTable();
            using (DBManager dbm = new DBManager("stp_Search_UOM", objSearch))
            {
                dt = dbm.GetDataTable();
                objSearch.TotalRecordCount = Convert.ToInt32(dbm.GetValueInInt("@TotalRecordCount"));
            }
            List<UOM> lstUOM = new List<UOM>();
            foreach (DataRow dr in dt.Rows)
            {
                UOM objUOMInfo = PopulateUOM(dr);
                lstUOM.Add(objUOMInfo);
            }
            return lstUOM;

        }

        /// <summary>
        /// Populate the UOM
        /// </summary>
        /// <param name="dr"> The UOMInfo DataRow </param>
        /// <returns>The UOM Object </returns>
        public UOM PopulateUOM(DataRow dr)
        {
            UOM objUOMInfo = new UOM();

            objUOMInfo.ID = Convert.ToInt32(dr["PK_UOMID"]);
            objUOMInfo.Name = Convert.ToString(dr["Name"]);
            objUOMInfo.IsActive = Convert.ToBoolean(dr["IsActive"]);
            objUOMInfo.LastModifiedBy = Convert.ToInt32(dr["LastModifiedBy"]);
            objUOMInfo.LastModifiedDate = Convert.ToDateTime(dr["LastModifiedDate"]);

            return objUOMInfo;
        }



    }
}